<?php

if(! defined('OT_ROOT')) {
	exit('Access Denied');
}



class Ca{

	// 广告缓存
	public static function MakeJs(){
		global $DB;

		$newStr='
		var showHiddenAd=false;
		if (typeof(webTypeName) == "undefined"){
			try { console.log("[webTypeName]未定义"); }catch(e){}
			webTypeName = "";
		}
		function OTca(str){
			switch (str){
			';

		$adStyleStr = '';
		$adexe=$DB->query('select * from '. OT_dbPref .'ad');
		while ($row = $adexe->fetch()){
			$AD_code = self::GetCode($row['AD_code'], $row['AD_groupID'], $row['AD_groupMode']);
			if ($row['AD_num'] >= 100 && $row['AD_num'] <= 199){
				$AD_code = str_replace(InfoImgAdminDir, '../wap/'. InfoImgAdminDir, $AD_code);
			}

			$newStr .= '
			case "ot'. Str::FixLen($row['AD_num'],3) .'":
			';
			if ($row['AD_state'] == 1){
				$adStyleStr .= '.ad'. $row['AD_num'] .'Style,.ca'. $row['AD_num'] .'Style {'. $row['AD_divStyle'] .'}';

				if (strlen(''. $row['AD_areaStr']) > 1){
					$newStr .= '
					if (("'. $row['AD_areaStr'] .'").indexOf("["+ webTypeName +"]")!=-1){
						'. OT::HtmlToJs($AD_code) .'
					}
					';
				}else{
					$newStr .= '
						'. OT::HtmlToJs($AD_code) .'
						';
				}
				$newStr .= '
				break;
				';
			}else{
				$newStr .= '
				if (showHiddenAd==true){
					'. OT::HtmlToJs($AD_code) .'
				}
				break;
				';
			}
		
		}
		unset($adexe);

		$newStr .= '
				}
			}
			';

		$retStr = str_replace('[siteTime]', OT_UPDATETIME, $newStr);
		$retStr = str_replace('[siteTitle]', '"+ encodeURIComponent(document.title) +"', $retStr);
		$retStr = str_replace(str_replace("/","\/",InfoImgAdminDir), "\"+ webPathPart +\"". str_replace("/","\/",InfoImgDir), $retStr);
		$retStr = str_replace('..\/wap\/"+ webPathPart +"', '"+ pcPathPart +"', $retStr);
		
		return File::Write( OT_ROOT .'cache/js/OTca.js' , $retStr . OT::HtmlToJs('<style type="text/css">'. $adStyleStr .'</style>') .'/* '. TimeDate::Get() .' */');
	}


	// 获取广告代码
	public static function GetCode($str, $groupID, $groupMode){
		global $DB;

		$retStr = '';
		if ($groupID > 0){
			$retStr .= self::GetGroupData($groupID);
			if ($groupMode == 10){
				$retStr .= $str;
			}elseif ($groupMode == 20){
				$retStr = $str . $retStr;
			}
		}else{
			$retStr = $str;
		}
		return $retStr;
	}


	// 获取广告组代码
	public static function GetGroupData($groupID){
		global $DB;

		$retStr = '';
		$todayTime = TimeDate::Get();
		$texe = $DB->query('select * from '. OT_dbPref .'caType where CT_groupID='. $groupID .' and CT_state=1 order by CT_rank ASC');
		while ($trs = $texe->fetch()){
			$dataNum = 0;
			$styleClass = '';
			if ($trs['CT_style'] == 1){
				$styleClass = 'style1';
			}

			$retStr .= $trs['CT_announ1'];

			$dexe = $DB->query('select * from '. OT_dbPref .'caData where CD_typeID='. $trs['CT_ID'] .' and CD_state=1 and (CD_isTime=0 or (CD_isTime=1 and CD_startTime<='. $DB->ForTime($todayTime) .' and CD_endTime>='. $DB->ForTime($todayTime) .')) order by CD_rank ASC');
			if ($trs['CT_type'] == 'img'){
				// 图片模式
				$retStr .= '<div class="OTcaArea '. $styleClass .'">';
				if ($trs['CT_col'] == 1){
					while ($drs = $dexe->fetch()){
						$addiStr = '';
						$imgWidth = $imgHeight = '100%';
						if (strlen(''. $drs['CD_imgWidth']) > 0){ $imgWidth = $drs['CD_imgWidth']; }
						if (strlen(''. $drs['CD_imgHeight']) > 0){ $imgHeight = $drs['CD_imgHeight']; }
						if (strpos($drs['CD_event'],'|nofollow|') !== false){ $addiStr .= ' rel="nofollow"'; }
						$retStr .= '<div><a href="'. $drs['CD_url'] .'" '. $addiStr .' style="" target="_blank"><img title="'. $drs['CD_theme'] .'" src="'. Info::InfoImgUrl($drs['CD_img'], InfoImgAdminDir) .'" style="display: block; width: '. $imgWidth .'; height: '. $imgHeight .';"></a></div>';
					}
				}else{
					while ($drs = $dexe->fetch()){
						$dataNum ++;
						if ($dataNum % 2 == 1){ $retStr .= '<span>'; }
						$addiStr = '';
						$imgWidth = $imgHeight = '100%';
						if (strlen(''. $drs['CD_imgWidth']) > 0){ $imgWidth = $drs['CD_imgWidth']; }
						if (strlen(''. $drs['CD_imgHeight']) > 0){ $imgHeight = $drs['CD_imgHeight']; }
						if (strpos($drs['CD_event'],'|nofollow|') !== false){ $addiStr .= ' rel="nofollow"'; }

						$retStr .= '<a href="'. $drs['CD_url'] .'" '. $addiStr .' style="" target="_blank"><img title="'. $drs['CD_theme'] .'" src="'. Info::InfoImgUrl($drs['CD_img'], InfoImgAdminDir) .'" style="display: block; width: '. $imgWidth .'; height: '. $imgHeight .';"></a>';
						if ($dataNum % 2 == 0){ $retStr .= '</span>'; }
					}
					if ($dataNum % 2 != 0){ $retStr .= '</span>'; }
				}
				$retStr .= '</div>';
			}else{
				// 文字模式
				$textArr = array();
				while ($drs = $dexe->fetch()){
					$textArr[$drs['CD_row']][$drs['CD_col']] = $drs;
				}

				$retStr .= '<div class="OTcaText '. $styleClass .'"><table class="OTcaTab '. $styleClass .'" align="center" border="0" cellpadding="0" cellspacing="5" style="'. $trs['CT_tabStyle'] .'">';
				for ($i=1; $i<=$trs['CT_row']; $i++){
					$retStr .= '<tr>';
					for ($n=1; $n<=$trs['CT_col']; $n++){
						if (isset($textArr[$i][$n])){
							$addiStr = '';
							if (strpos($textArr[$i][$n]['CD_event'],'|nofollow|') !== false){ $addiStr .= ' rel="nofollow"'; }
							$retStr .= '<td style="text-align:center;'. $trs['CT_tdStyle'] .'"><a href="'. $textArr[$i][$n]['CD_url'] .'" style="'. $textArr[$i][$n]['CD_themeStyle'] .'" '. $addiStr .' target="_blank">'. $textArr[$i][$n]['CD_theme'] .'</a></td>';
						}else{
							$retStr .= '<td style="text-align:center;color:#b5b1b4;'. $trs['CT_tdStyle'] .'">'. $trs['CT_tdDefStr'] .'</td>';
						}
					}
					$retStr .= '</tr>';
				}
				$retStr .= '</table></div>';
			}
			$dexe = null;

			$retStr .= $trs['CT_announ2'];
		}
		unset($texe);

		return $retStr;
	}


	// 广告缓存提示化
	public static function MakeTishiJs(){
		global $DB;

		$newStr='
		var showHiddenAd=false;
		function OTca(str){
			switch (str){
			';

		$adStyleStr = '';
		$adexe=$DB->query('select AD_num,AD_theme,AD_divStyle from '. OT_dbPref .'ad');
		while ($row = $adexe->fetch()){
			$newStr .= '
			case "ot'. Str::FixLen($row['AD_num'],3) .'":
			';
				$adStyleStr .= '.ad'. $row['AD_num'] .'Style,.ca'. $row['AD_num'] .'Style {'. $row['AD_divStyle'] .'}';

				$newStr .= '
				'. OT::HtmlToJs('<table style="width:100%;border:2px blue dashed;background:#cdcdfe;"><tr><td align="center" style="padding:15px 0 15px 0;font-size:14px;color:blue;">编号：'. $row['AD_num'] .'，'. $row['AD_theme'] .'</td></tr></table>') .'
				break;
				';
		}
		unset($adexe);

		$newStr .= '
				}
			}
			';

		return File::Write( OT_ROOT .'cache/js/OTca.js' , $newStr . OT::HtmlToJs('<style type="text/css">'. $adStyleStr .'</style>') );
	}


	// 获取指定编号广告代码
	public static function GetPointCode($adID, $webTypeName){
		global $DB;

		$retStr = '';
		$row = $DB->GetRow('select AD_groupID,AD_groupMode,AD_code,AD_areaStr from '. OT_dbPref .'ad where AD_num='. $adID .' and AD_state=1');
		if ($row){
			// if (! (strlen(''. $row['AD_areaStr']) > 1 && strpos(''. $row['AD_areaStr'],'['. $webTypeName .']') === false)){
			if (strlen(''. $row['AD_areaStr']) < 1 || (strlen(''. $row['AD_areaStr']) > 1 && strpos($row['AD_areaStr'],'['. $webTypeName .']') !== false)){
				$retStr = Ca::GetCode($row['AD_code'], $row['AD_groupID'], $row['AD_groupMode']);
			}
		}
		unset($adexe);

		return $retStr;
	}

}
?>